"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.streamLastPaid = exports.insertDailyData = exports.insertHourData = exports.insertFiveData = void 0;
const axios_1 = __importDefault(require("axios"));
const _1 = require(".");
const utils_1 = require("../utils");
/**
 * 五分钟添加一次
 * @param fiveClient five_report集合
 * @param lastClient last_paid集合
 * @param faucetClient faucet集合
 */
function insertFiveData(config) {
    return __awaiter(this, void 0, void 0, function* () {
        /**
        * 格式化时间
        */
        const clients = yield _1.createDBClient(config.config);
        let insertTime = utils_1.initTime();
        let res;
        try {
            res = yield axios_1.default.get("https://faucetpay.io/api/listv1/faucetlist");
        }
        catch (error) {
            console.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        }
        const data = res.data.list_data.normal;
        for (const i in data) {
            for (const n in data[i]) {
                let item = data[i][n];
                //查询faucet集合是否存在指定数据
                let faucet = yield getOrinsertFaucet(clients.faucetClient, item);
                //处理last_paid集合
                yield streamLastPaid(item, clients.lastClient, insertTime, faucet);
                const insertData = utils_1.initDataFive(item, faucet._id, insertTime);
                yield clients.fiveClient.insert(insertData);
            }
        }
        console.log("five_report集合添加成功！");
    });
}
exports.insertFiveData = insertFiveData;
//每小时添加
function insertHourData(config) {
    return __awaiter(this, void 0, void 0, function* () {
        const clients = yield _1.createDBClient(config);
        let hour_data = yield utils_1.initDataHour(clients);
        if (hour_data.length == 0) {
            return;
        }
        yield clients.hourClient.insertMany(hour_data);
        console.log("hour_report集合添加成功！");
    });
}
exports.insertHourData = insertHourData;
//每天添加
function insertDailyData(config) {
    return __awaiter(this, void 0, void 0, function* () {
        const clients = yield _1.createDBClient(config);
        const daily_data = yield utils_1.initDataDaily(clients);
        yield clients.dailyClient.insertMany(daily_data);
        console.log("daily_report集合添加成功！");
    });
}
exports.insertDailyData = insertDailyData;
/**
* operationTime：新增数据或者paid_today改变时 的日期
* 根据主键查数据。
*     没有数据： 添加，退出
*     有数据 ： 数据库取paid_today与接口数据比较
*                  改变了：  last_paid_mins 固定5
*                  没改变： 当前时间的毫秒值减去数据库operationTime的毫秒值  除以6W 得到分钟即为支付间隔
* @param data 接口数据
* @param lastClient last_paid集合
* @param insertTime 插入时间
* @param faucetDatas faucet集合数据
* @returns
*/
function streamLastPaid(data, lastClient, insertTime, faucetDatas) {
    return __awaiter(this, void 0, void 0, function* () {
        //根据主键查询数据
        const last_paid = yield lastClient.getOne({ condition: { "faucet_id": faucetDatas._id } });
        /**
        * 不存在数据就insert数据，并且返回insert的数据。
        */
        if (!last_paid) {
            let lastPaid = utils_1.initDataLastPaid(data, insertTime, faucetDatas._id);
            yield lastClient.insert(lastPaid);
            return lastPaid;
        }
        else {
            const api_paid_today = data.paid_today;
            const db_paid_today = last_paid.paid_today;
            let last_paid_mins = 5;
            if (api_paid_today == db_paid_today) {
                const last_paid_operationTime = last_paid.operationTime.getTime();
                const now_insertTime = insertTime.getTime();
                last_paid_mins = (now_insertTime - last_paid_operationTime) / 60000;
                insertTime = last_paid.operationTime;
            }
            yield lastClient.updateOne({ "_id": last_paid._id, "faucet_id": faucetDatas._id, "paid_today": api_paid_today, "operationTime": insertTime, "last_paid_mins": last_paid_mins });
            return last_paid;
        }
    });
}
exports.streamLastPaid = streamLastPaid;
/**
 * 获取数据或者添加数据
 *
 * @param faucetClient
 * @param item
 * @returns
 */
function getOrinsertFaucet(faucetClient, item) {
    return __awaiter(this, void 0, void 0, function* () {
        let faucet;
        const faucet_data = yield faucetClient.getOne({ condition: { "id": item.id, "currency": item.currency } });
        if (!faucet_data) {
            faucet = utils_1.initDataFaucet(item);
            yield faucetClient.insert(faucet);
            return faucet;
        }
        return faucet_data;
    });
}
